/**
 * @author 徐航
 * @file  loading界面
 */
cc.Class({
    extends: cc.Component,
    properties: {
        tipView: cc.Node,
        loadProgress: cc.ProgressBar,
        descText: cc.Label,
        socket: require('socketManager'),
        adsManager: require('adsManager'),
        redBag: require('redBagData')
    },

    start() {
        this.isDeBug = true;
        this._json = {};
        this.tipView.active = false;
        this.Test();
        // if(cc.sys.localStorage.getItem('agree') == 0) {
        //     this.tipView.active = false;
        //     this.Test();
        // }
        // else {
        //     this.tipView.active = true;
        // }
        // cc.director.preloadScene('scene')
    },

    onEnable () {
        cc.director.on('Register', this.onRegisterHandler, this);
        cc.director.on('PostInit', this.onPostInitHandler, this);
        cc.director.on('postLoginRecord', this.onPostLoginRecordHandler, this)
    },

    onDisable () {

    },

    onRegisterHandler() {
        this.loadProgress.progress = 0.1;
        this.descText.string = "初始化信息";
        this.socket.postInitHandler((res)=>{
            cc.director.emit('PostInit', true);
        })
    },

    onPostInitHandler() {
        this.loadProgress.progress = 0.6;
        this.descText.string = "初始化成功";
        this.socket.postLoginRecordHandler(()=>{
            cc.director.emit('postLoginRecord', true);
        })
    },

    onPostLoginRecordHandler() {
        this.loadProgress.progress = 1;
        this.descText.string = "登录游戏";
        this.redBag.requestConfigs(()=>{
            cc.director.loadScene('scene');
            // this.pageMgr.onOpenPage(1)
            // this.game.gameStart()
        })
    },


    btnOkClick() {
        if(!this.isDeBug) {

        }
        else {
            this.tipView.active = false;
            this.Test();
            cc.sys.localStorage.setItem('agree', 0);
        }
    },

    Test(){
        console.log("请求游戏配置")
        // if(cc.sys.localStorage.getItem('testInfo')) {
        //     this._json = JSON.parse(cc.sys.localStorage.getItem('testInfo'));
        //     //var xhr = cc.loader.getXMLHttpRequest();
        // }
        // else {
        //     this._json["plan_id"] = "540056";
        //     this._json["package_id"] = "";
        //     this._json["device_model"] = "Test " + Math.ceil(Math.random() * 100000);
        //     this._json["device_system"] = "29";
        //     this._json["androidId"] = "444db12299b21" + Math.ceil(Math.random() * 99999);
        //     this._json["package_version"] = "0.1";
        //     this._json["package_identifier"] = "com.dgxxl";
        //     this._json["imei"] = " ";
        //     this._json["oaid"] = " ";
        //     this._json["mac"] = "" ;
        //     this._json['device_uuid'] = '444db12299b21'
        //     this._json["device_data"] = "20";
        //     this._json["tag"] = "";
        //     this._json['idfv'] = ' ';
        //     this._json['idfa'] = ' ';
        //     this._json['openid'] = '100100100';
        //     this._json['x'] = '1';
        //     this._json['y'] = '2';
            
        //     cc.sys.localStorage.setItem('testInfo', JSON.stringify(this._json));
        // }
        this.socket.register(this._json, (res)=>{
            // cc.sys.localStorage.setItem('user_id', res.user_id);
            // cc.sys.localStorage.setItem('package_id', res.package_id);
            // this.adsManager.reloadData(res.ad_space_list);
            // cc.sys.localStorage.setItem('securityLevel', res.s_level);
            // cc.director.emit('Register', true);
        });
    }
})